package com.ptrans.server;

import static com.ptrans.utilities.CommonUtilities.*;

import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Node;

import android.content.Context;
import android.util.Log;

import com.google.android.gcm.GCMRegistrar;
import com.ptrans.R;
import com.ptrans.modele.User;
import com.ptrans.utilities.ParserXML;

/**
 * Class allowing to handle user on server
 * @author Jean
 *
 */
public class UserHandler {

	/**
	 * Set the user position on server
	 * @param context
	 * @param user
	 * @return
	 */
	public static boolean setPosition(final Context context, User user) {
        Log.i(TAG, context.getString(R.string.setting_position));
        boolean b = false;
        String serverUrl = SERVER_URL;
        Node response;
        String message;
        Map<String, String> params = new HashMap<String, String>();
        params.put(PARAM_CONTROLLER, C_REGISTER);
        params.put(PARAM_ACTION, A_SET_POSITION);
        params.put(PARAM_USER_NAME, user.getName());
        params.put(PARAM_REGID, user.getRegId());
        params.put(PARAM_POSITION, ((Integer)user.getPosition()).toString());
        
        response = ServerHttp.tryAConnection(serverUrl, params, context);
        
        if(response != null) {       
	        if(!ParserXML.getState(response).equals("")) {
	            message = context.getString(R.string.set_position);
	            b = true;
	        } else {
	        	message = context.getString(R.string.set_position_error);
	        }
	        Log.i(TAG, message);
	        return b;
        }
        
        message = context.getString(R.string.set_position_error);
        Log.i(TAG, message);
        return b;
    }
	
	/**
	 * Set the health user on server
	 * @param context
	 * @param user
	 * @return
	 */
	public static boolean setHealth(final Context context, User user) {
        Log.i(TAG, context.getString(R.string.setting_health));
        boolean b = false;
        String serverUrl = SERVER_URL;
        Node response;
        String message;
        Map<String, String> params = new HashMap<String, String>();
        params.put(PARAM_CONTROLLER, C_REGISTER);
        params.put(PARAM_ACTION, A_SET_HEALTH);
        params.put(PARAM_USER_NAME, user.getName());
        params.put(PARAM_REGID, user.getRegId());
        params.put(PARAM_HEALTH, ((Integer)user.getHealth()).toString());
        
        response = ServerHttp.tryAConnection(serverUrl, params, context);
        
        if(response != null) {       
	        if(!ParserXML.getState(response).equals("")) {
	            message = context.getString(R.string.set_health);
	            b = true;
	        } else {
	        	message = context.getString(R.string.set_health_error);
	        }
	        Log.i(TAG, message);
	        return b;
        }
        
        message = context.getString(R.string.set_health_error);
        Log.i(TAG, message);
        return b;
    }
	
	/**
	 * Check if the user name exist on server
	 * @param context
	 * @param user
	 * @return
	 */
	public static boolean isNameExist(final Context context, User user) {
        Log.i(TAG, context.getString(R.string.checking_existing_name));
        boolean b = false;
        String serverUrl = SERVER_URL;
        Node response;
        String message;
        Map<String, String> params = new HashMap<String, String>();
        params.put(PARAM_CONTROLLER, C_REGISTER);
        params.put(PARAM_ACTION, A_VERIFY_NAME);
        params.put(PARAM_USER_NAME, user.getName());
        
        response = ServerHttp.tryAConnection(serverUrl, params, context);
        
        if(response != null) {       
	        if(!ParserXML.getState(response).equals("")) {
	            message = context.getString(R.string.name_not_exists);
	            b = true;
	        } else {
	        	message = context.getString(R.string.name_exists);
	        }
	        Log.i(TAG, message);
	        return b;
        }
        
        message = context.getString(R.string.verify_name_error);
        Log.i(TAG, message);
        return b;
    }
	
}
